import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:furaha_mkopo/commonUI/fm_life_visibility_page.dart';
import 'package:furaha_mkopo/commonUI/fm_refresh_header.dart';
import 'package:furaha_mkopo/page/base/fm_base_page_widget.dart';
import 'package:furaha_mkopo/page/home/controller/fm_common_controller.dart';
import 'package:furaha_mkopo/page/home/controller/fm_home_controller.dart';
import 'package:furaha_mkopo/page/home/enum/fm_home_net_enum.dart';
import 'package:furaha_mkopo/page/home/enum/fm_home_page_enum.dart';
import 'package:furaha_mkopo/page/home/fm_home_large_card_slot_page.dart';
import 'package:furaha_mkopo/page/home/fm_home_small_card_slot_page.dart';
import 'package:geolocator_platform_interface/src/models/position.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class FMHomePage extends FMBasePageWidget {
  const FMHomePage({super.key});

  @override
  State<FMHomePage> createState() => _FMHomePageState();
}

class _FMHomePageState extends FMBasePageState<FMHomePage> {
  final controller = Get.put(FMHomeController());
  final commonController = Get.put(FMCommonController());
  final refreshController = RefreshController();

  @override
  void initState() {
    super.initState();
    controller.onRefreshData(reconnectCount: 3);

    controller.checkNeedPopLocation();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FMHomeController>(
      builder: (controller) {
      return Container(
          padding: EdgeInsets.only(top: MediaQuery.of(context).viewPadding.top),
          decoration: BoxDecoration(
            image: controller.getDecorationImage(),
          ),
          child: FMLifeVisibilityPage(
            parentContext: context,
            viewWillAppear: () {
              controller.onRefreshData(reconnectCount: 3);
            },
            child: SmartRefresher(
              enablePullUp: false,
              enablePullDown: true,
              controller: refreshController,
              header: const FMRefreshHeader(),
              onRefresh: () {
                controller.onRefreshData(refreshController: refreshController);
              },
              child: generatePage(),
            ),
          ),
        );
      }
    );

  }

  Widget generatePage(){
    switch (controller.netEnum) {
      case FMHomeNetEnum.loading:
        return Container();
      case FMHomeNetEnum.success:
        if (controller.pageEnum == FMHomePageEnum.big) {
          return FMHomeLargeCardSlotPage(
              beamedEntity: controller.beamedEntity!);
        }else {
          return FMHomeSmallCardSlotPage(
            fmHomeEntity: controller.fmHomeEntity!,
          );
        }
      case FMHomeNetEnum.error:
        return const Center(child: Text("Error"));
    }
  }

  @override
  void onLocationError(String error) {}

  @override
  void onLocationUpdated(Position position) {
    print(position);
  }
}
